package com.gzhryc.shared_device.oem.h5.mng.controllers.market.account;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.net_marketing.cloud.account.dao.db.CloudPayAccount;
import com.gzhryc.net_marketing.cloud.account.dao.enums.CloudPayAccountOperateRecordEnum;
import com.gzhryc.net_marketing.cloud.account.services.CloudPayAccountService;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.net_marketing.cloud.account.dao.db.CloudPayAccountExpenditureOrder;
import com.gzhryc.net_marketing.cloud.account.services.CloudPayAccountExpenditureOrderService;
import com.gzhryc.net_marketing.cloud.account.services.dto.CloudPayAccountExpenditureOrderSearch;
import com.gzhryc.system.SysConstants;
import com.gzhryc.system.dao.db.Manager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class CloudPayAccountExpenditureOrderController {

    @Route(value = { "/account/expenditureOrder/list.html" })
    public String list(HttpServletRequest request) {
        return "/account/expenditureOrder/list.html";
    }

    @Route(value = { "/account/expenditureOrder/list.data" })
    public ListResult<CloudPayAccountExpenditureOrder> listData(HttpServletRequest request) {
        ListResult<CloudPayAccountExpenditureOrder> result = new ListResult<CloudPayAccountExpenditureOrder>(DtoConstants.NULL_DATA, "暂无数据");
        CloudPayAccountExpenditureOrderSearch search = WebUtils.getParameter(request, CloudPayAccountExpenditureOrderSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(CloudPayAccountExpenditureOrderService.self().countBySearch(search));
        result.getBody().setData(CloudPayAccountExpenditureOrderService.self().findBySearch(search, pageNo, pageLimit));

        if (result.getBody().getData() != null) {
            result.setCode(DtoConstants.SUCCESS, null);
        }
        return result;
    }

    @Route(value = { "/account/expenditureOrder/info.html" })
    public String info(HttpServletRequest request) {
        String tradeNo = WebUtils.getString(request,"tradeNo");
        CloudPayAccountExpenditureOrder expenditureOrder = CloudPayAccountExpenditureOrderService.self().getByTradeNo(tradeNo);
        if(expenditureOrder != null) {
            WebUtils.setAttribute(request, expenditureOrder);
            CloudPayAccount account = CloudPayAccountService.self().getById(expenditureOrder.getAccountId());
            if(account != null){
                request.setAttribute("account",account);
            }
            CloudPayAccount payeeAccount = CloudPayAccountService.self().getById(expenditureOrder.getPayeeAccountId());
            if(payeeAccount != null){
                request.setAttribute("payeeAccount",payeeAccount);
            }
        }
        return "/account/expenditureOrder/info.html";
    }

    @Route(value = { "/account/expenditureOrder/callback.do" })
    public AjaxResult doCallback(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"触发回调失败，请稍后再试");
        String tradeNo = WebUtils.getString(request,"tradeNo");
        try {
            CloudPayAccountExpenditureOrderService.self().callback(tradeNo);
            result.setCode(DtoConstants.SUCCESS,null);
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/account/expenditureOrder/settlement.do" })
    public AjaxResult doSettlement(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"触发回调失败，请稍后再试");
        String tradeNo = WebUtils.getString(request,"tradeNo");
        String reason = WebUtils.getString(request,"reason");
        Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
        try {
            CloudPayAccountExpenditureOrder expenditureOrder = CloudPayAccountExpenditureOrderService.self().getByTradeNo(tradeNo);
            if(expenditureOrder != null) {
                if (CloudPayAccountExpenditureOrderService.self().settlement(expenditureOrder,CloudPayAccountOperateRecordEnum.OperatorType.Manager.index()
                        ,manager.getFullName(),reason)) {
                    result.setCode(DtoConstants.SUCCESS);
                }
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
